package com.inforich.web.controller.company;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Arrays;
import cn.hutool.core.bean.BeanUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.BusinessType;
import com.inforich.company.domain.TransportCompany;
import com.inforich.company.service.ITransportCompanyService;
import com.inforich.common.utils.poi.ExcelUtil;

/**
 * 清运公司Controller
 *
 * @author hjm
 * @date 2022-10-23
 */
@RestController
@RequestMapping("/company/company")
public class TransportCompanyController extends BaseController {

  @Autowired
  private ITransportCompanyService transportCompanyService;

  /**
   * 所有公司
   */
  @GetMapping("/all")
  public AjaxResult all() {
    List<TransportCompany> list = transportCompanyService.listTransportCompany(new HashMap<>());
    return AjaxResult.success(list);
  }

  /**
   * 查询清运公司列表
   */
  @PreAuthorize("@ss.hasPermi('company:company:list')")
  @GetMapping("/list")
  public Page<TransportCompany> list(Page<TransportCompany> page,
      TransportCompany transportCompany) {
    Map<String, Object> map = beanToMap(transportCompany);
    transportCompanyService.pageTransportCompany(page, map);
    return page;
  }


  /**
   * 导出清运公司列表
   */
  @PreAuthorize("@ss.hasPermi('company:company:export')")
  @Log(title = "清运公司", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, TransportCompany transportCompany) {
    Map<String, Object> map = beanToMap(transportCompany);
    List<TransportCompany> list = transportCompanyService.listTransportCompany(map);
    ExcelUtil<TransportCompany> util = new ExcelUtil<TransportCompany>(TransportCompany.class);
    util.exportExcel(response, list, "清运公司数据");
  }

  /**
   * 获取清运公司详细信息
   */
  @PreAuthorize("@ss.hasPermi('company:company:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") String id) {
    return AjaxResult.success(transportCompanyService.getTransportCompanyById(id));
  }

  /**
   * 新增清运公司
   */
  @PreAuthorize("@ss.hasPermi('company:company:add')")
  @Log(title = "清运公司", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody TransportCompany transportCompany) {
    String checkData = transportCompanyService.checkData(transportCompany);
    if (StrUtil.isNotBlank(checkData)) {
      return AjaxResult.error(checkData);
    }
    return toAjax(transportCompanyService.insertTransportCompany(transportCompany));
  }

  /**
   * 修改清运公司
   */
  @PreAuthorize("@ss.hasPermi('company:company:edit')")
  @Log(title = "清运公司", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody TransportCompany transportCompany) {
    String checkData = transportCompanyService.checkData(transportCompany);
    if (StrUtil.isNotBlank(checkData)) {
      return AjaxResult.error(checkData);
    }
    return toAjax(transportCompanyService.updateTransportCompany(transportCompany));
  }

  /**
   * 删除清运公司
   */
  @PreAuthorize("@ss.hasPermi('company:company:remove')")
  @Log(title = "清运公司", businessType = BusinessType.DELETE)
  @DeleteMapping("/{id}")
  public AjaxResult remove(@PathVariable String id) {
    return transportCompanyService.removeTransportCompany(id);
  }
}
